package c2.advanced.business.common.video;

import c2.advanced.share.RpcSuit;
import cate.common.rpc.path.RpcCommonPath;
import cate.common.table.d.GDVideoHall;
import cate.game.play.proce.play.PlayPO;
import cate.game.videohall.routing.VideoRoutingMsg;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;


@Slf4j
public class VideoHallHandler {
    protected RpcSuit rpcSuit;

    public static VideoHallHandler INSTANCE = new VideoHallHandler();

    private VideoHallHandler() {

    }

    public void setUp(RpcSuit rpcSuit) {
        this.rpcSuit = rpcSuit;
    }

    public void handleVideo(GDVideoHall.Group group, PlayPO playPO, boolean addedBoth) {
        if (StringUtils.hasLength(playPO.a.snapshot.uid)) {
            handlePlay(group, playPO, playPO.a.snapshot.base.srvId, playPO.a.snapshot.uid);
        }
        if (StringUtils.hasLength(playPO.b.snapshot.uid) && addedBoth) {
            handlePlay(group, playPO, playPO.b.snapshot.base.srvId, playPO.b.snapshot.uid);
        }
    }

    private void handlePlay(GDVideoHall.Group group, PlayPO playPO, int srvId, String uid) {
        try{
            rpcSuit.send().fireAndForgetGameTemplate(RpcCommonPath.VIDEO_CROSS_TO_GAME,
                    srvId,
                    VideoRoutingMsg.of(uid, group, playPO));
        } catch (Exception e) {
            log.error("发送剧本给game srv异常", e);
        }
    }
}
